- /* slflsmul.cpp by K.Tsuru */
- // function ID = 212 DRADIX(for BRADIX use IsMult )
- /************************************************************
- SLong class
- m*n
- It multiplies m by a small number n where 0 <= n < ULONG_MAX.
- **************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- SLong LsMult(const SLong& m, ulong n){
- const ulong mt = m.SlOpMaxValue();
- if(n > mt){
- SLong s(m.Type(), 0);
- s = n;
- return NLLMult(m, s);
- // m.SetError(m.OUT_OF_RANGE,"LsMult", 212);
- }
- if(n == 1uL) return m;
-
- SLong result(m.Type(), 0);
- uint rh = m.aHead, rt = m.aTail, i;
- if( !n || !m.Sign(212) ){
- result.SetZero();
- return result;
- }
-
- result.valloc(rh+1, -1);
- if(rt) result.figure.clear(0, rt-1);
- fType* rv = result.figure.Elements();
- const fType* mv = m.ReadFigures();
- ulong w, u = 0, rdx = m.Radix();
-
- for(i = rt; i <= rh ; i++){
- w = (ulong)mv[i]*n + u;
- u = w/rdx; //carry
- rv[i] = fType(w - u*rdx); //faster than rv[i] = fType(w%rdx);
- }
- if(u){//carry to the top figure
- if(u < rdx){//lass than radix
- rh++;
- result.Reserve(rh);
- result.figure[rh] = (fType)u;
- rv = result.figure.Elements();
- } else {//greater than radix
- //it normalizes the carry and copy to the top position.
- fType temp[5];
- i = 0;
- while(u){
- temp[i] = fType(u%rdx);
- u /= rdx; i++;
- }
- result.Reserve(rh+i); // result.size() >= rh+i+1
- rv = result.figure.Elements();
- memcpy( (rv+rh+1), temp, i*sizeof(fType));
- rh += i;
- }
- }
- //It gets the figure position.
- result.aHead = rh;
- result.figure.clear(rh+1);
-
- while(!rv[rt]) rt++;
- result.aTail = rt;
- result.SetSign( m.Sign() );
- return result;
- }
slflsmul.cpp : last modifiled at 2017/03/13 14:32:00(1,735 bytes)
created at 2017/10/07 10:26:49
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).